﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SDT.Libraries.TagRanking
{
    public class KeWordCounter
    {
        public int CountWord(string content, string keyword)
        {
            //doi voi tu
            //khoi tao bien dem
            int count = 0;
            if (!keyword.Contains(' '))
            {
                //cat chuoi thanh tung phan tu cua mang
                string[] arrContent = content.Split(' ');
                //duyet mang so sanh keyword
                for (int i = 0; i < arrContent.Length; i++)
                {
                    if (keyword.Equals(arrContent[i]))
                    {
                        count = count + 1;
                    }
                }
                return count;
            }
            else
            {
                //doi voi cum tu
                //
                int i = 0;
                while (content.Length > 0)
                {
                    if (keyword.Length > content.Length)
                    {
                        break;
                    }
                    if (content.IndexOf(keyword) < 0)
                    {
                        break;
                    }
                    else
                    {
                        i = content.IndexOf(keyword);
                        content = content.Substring(i + keyword.Length);
                        count = count + 1;
                    }

                }
                return count;

            }
        }
    }
}
